package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.cache.util.RedisKeys;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
    public static void addCookie(String name, String value, int time, HttpServletResponse response){
       Cookie c=   new Cookie(name,value);
       c.setMaxAge(time);
        c.setPath("/");
        response.addCookie(c);
    }
public  static  String getToken(HttpServletResponse response , HttpServletRequest request){
    Cookie[] cookies = request.getCookies();
    String token=null;
    if(cookies !=null && cookies.length>0){
        for (Cookie cookie :cookies){
            if ("token".equals(cookie.getName())){
                token=cookie.getValue();
                cookie.setPath("/");
                cookie.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
                response.addCookie(cookie);
                break;
            }

        }
    }
return token;
}

}
